﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using GroupCheer.FBGraphAPI;

namespace GroupCheer.FBGraphAPI.Core
{
    public class PostFileData
    {
        public string Name;
        public string FileName;
        public string ContentType;
        public byte[] FileData;
    }

    public class Helpers
    {
        public static byte[] BuildMultipartPostData(string Boundary, Dictionary<string, string> HttpPostData, PostFileData FileUploadData)
        {
            StringBuilder sb = new StringBuilder();

            // append access token.
            sb.AppendLine("------" + Boundary);
            sb.AppendLine("Content-Disposition: form-data;name=\"access_token\"");
            sb.Append(Environment.NewLine);
            sb.AppendLine(FBGraphAPIConfiguration.FBValidAccessToken);

            // append form part.
            if (HttpPostData != null && HttpPostData.Count > 0)
            {
                foreach (KeyValuePair<string, string> HttpPostDataItem in HttpPostData)
                {
                    sb.AppendLine("------" + Boundary);
                    sb.AppendLine(string.Format("Content-Disposition: form-data;name=\"{0}\"", HttpPostDataItem.Key));
                    sb.Append(Environment.NewLine);
                    sb.AppendLine(HttpPostDataItem.Value);
                }
            }

            // handle file upload.
            if (FileUploadData != null)
            {
                sb.AppendLine("------" + Boundary);
                sb.AppendLine(string.Format("Content-Disposition: form-data;name=\"{0}\"; filename=\"{1}\"", FileUploadData.Name, FileUploadData.FileName));
                sb.AppendLine(string.Format("Content-Type: {0}", FileUploadData.ContentType));
                sb.Append(Environment.NewLine);
            }

            MemoryStream ms = new MemoryStream();
            BinaryWriter bw = new BinaryWriter(ms);
            bw.Write(Encoding.Default.GetBytes(sb.ToString()));
            bw.Write(FileUploadData.FileData);
            bw.Write(Encoding.Default.GetBytes(Environment.NewLine));
            bw.Write(Encoding.Default.GetBytes("------" + Boundary));
            ms.Flush();
            ms.Position = 0;

            byte[] result = ms.ToArray();

            bw.Close();

            return result;
        }

        public static void DefaultExecuteStateChangedHandler(object sender, FBGraphAPIEventStateChangedEventArgs e)
        {
            Console.WriteLine(e.EventData);
        }

        public static void DefaultExecuteCompletedHandler(object sender, FBGraphAPIEventCompletedEventArgs e)
        {
            if (e.isCompleted)
            {
                Console.WriteLine("Action Completed.");
                Console.WriteLine(e.EventData);
            }
            else
            {
                Console.WriteLine("Action failed.");

                if (e.LastException is FBGraphAPIException)
                {
                    Console.WriteLine("type: {0}", (e.LastException as FBGraphAPIException).Type);
                    Console.WriteLine("message: {0}", (e.LastException as FBGraphAPIException).Message);
                }
                else
                {
                    Console.WriteLine("message: {0}", e.LastException.Message);
                }
            }
        }
    }
}
